//
//  ChangePasswordViewController.m
//  IFengNews
//
//  Created by lanou3g on 16/3/7.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "ChangePasswordViewController.h"

@interface ChangePasswordViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *oldPassWord;
@property (weak, nonatomic) IBOutlet UITextField *newpasswords;
@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
//点击修改按钮
- (IBAction)changePassWord:(UIButton *)sender {
    NSUserDefaults *use = [NSUserDefaults standardUserDefaults];
    if ([use objectForKey:self.userName.text] == nil) {
        //账户名不存在
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"账户名未注册,请重新输入" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:alert animated:YES completion:^{
            sleep(1);
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
    }else{
        if ([[use objectForKey:self.userName.text] isEqualToString:self.oldPassWord.text])
        {
            
            if ([self.newpasswords.text length] != 0 ) {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码修改成功" preferredStyle:(UIAlertControllerStyleAlert)];
                [self presentViewController:alert animated:YES completion:^{
                    sleep(1);
                    //将新密码记录并保存
                    [use setValue:self.newpasswords.text forKey:self.userName.text];
                    [self dismissViewControllerAnimated:YES completion:^{
                        [self dismissViewControllerAnimated:YES completion:nil];
                    }];
                }];
            }else{
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"新密码不能为空，请重新输入" preferredStyle:(UIAlertControllerStyleAlert)];
                [self presentViewController:alert animated:YES completion:^{
                    sleep(1);
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
            }
        }else{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"旧密码输入错误，请重新输入" preferredStyle:(UIAlertControllerStyleAlert)];
            [self presentViewController:alert animated:YES completion:^{
                sleep(1);
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
        }
    }
}
//退出修改密码界面
- (IBAction)escChangeView:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
